//答题界面
// pages/txtedit.js
var app = getApp()
var ontimer;
var tim;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    syWidth: "", //系统宽度
    syHeight: "", //系统高度

    anbtWW: "", //答案按钮宽度
    anbtHH: "", //答案按钮高度
    tibtWW: "", //下一题按钮宽度
    tibtHH: "", //下一题按钮高度
    num: "", //题目号
    timu: "",
    ansa: "",
    ansb: "",
    ansc: "",
    ansd: "",
    anstrue: "",
    tishi: "", //倒计时提示
    flag: true, //已答完
    datifun: "", //答题事件
    modeC: false, //是否是练习模式
    bgcss: "bg", //背景

    timer: 200, //计时器
    userwxid: "", //当前微信ID
    mode: "", //当前模式或者当前账号
    jishu: 1, //题目计数
    fsJiShu: 0, //分数计数
    timerC: 0, //时间累积

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("进入答题页面-------------------------------")
    var that = this
    //获取设备宽高
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          syWidth: res.windowWidth,
          syHeight: res.windowHeight,
          anbtWW: res.windowWidth * 0.95,
          anbtHH: (res.windowWidth * 0.95 * 66) / 703,
          tibtWW: res.windowWidth * 0.95,
          tibtHH: (res.windowWidth * 0.95 * 92) / 703,
          mode: options.mode
        })
      }
    })
    console.log("当前答题人员:" + options.mode)
    var duiwustr = options.mode;
    if (duiwustr == "lianxi") {
      that.setData({
        modeC: true,
        bgcss: "lxbg"
      })
      console.log("开启练习模式:" + that.data.modeC)
      that.loaddata();

    } else {
      console.log("开启参赛模式:" + duiwustr)
      that.getuserwxid();
      console.log("获得用户WXID:" + app.globalData.userwxid)
    }

    //this.loaddata();//加载题目
    //this.countdown();//开启计时器

  },
  //获取用户wxid
  getuserwxid: function() {
    var that = this;
    wx.login({
      success: function(res) {
        if (res.code) {
          console.log("CODE:" + res.code)
          //发起网络请求
          wx.request({
            url: 'https://www.citytk.com/citytk/mgbs/modeht/code.php',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              code: res.code
            },
            method: "POST",
            success: function(res) {

              if (res.data != "nowxid") {

                app.globalData.userwxid = res.data;
                var _wxid = app.globalData.userwxid;
                if (_wxid != "") {
                  that.setData({
                    userwxid: _wxid,
                  })
                  that.checkuserforwxid(_wxid); //校验用户微信ID

                } else {
                  if (options.mode != "lianxi") {
                    wx.showToast({
                      title: '未获取到用户名，请联系管理员!',
                      icon: 'none',
                      duration: 10000
                    })
                  }
                }
                console.log("获得用户WXID:" + app.globalData.userwxid)

              } else {
                console.log('没有获取到wxid' + res.errMsg)
              }
            },
            fail: function(e) {
              console.log("用户登录失败:" + e.errMsg)
            }
          })
        } else {

          console.log('登录失败！' + res.errMsg)
        }
      }
    });

  },
  //提示器
  messhow: function(str) {
    wx.showToast({
      title: str,
      icon: 'none',
      duration: 3000
    })
  },
  //计时器
 countup:function(){
   var that=this;
   
   tim = setTimeout(function () {
     
    }, 3000);
  },
  //答题满10题跳转
  urlsales:function () {
    var that=this;

    //答题满10题
    clearTimeout(ontimer);
    wx.reLaunch({
      url: '../sales/sales?timer=' + that.data.timerC + '&fsJiShu=' + that.data.fsJiShu + '&modeC=' + that.data.modeC,
    })
  },
  //倒计时器
  countdown: function() {
    var that = this;
    if (that.data.timer > 0) {
      ontimer = setTimeout(function() {
       // console.log("时间：" + that.data.timer);
        that.setData({
          timer: that.data.timer - 1,
          tishi: "剩余时间" + Math.floor(that.data.timer / 100) + "秒",
        })
        that.countdown();
      }, 10);
    } else {
      var _jishu = that.data.jishu + 1;
      that.setData({
        jishu: _jishu
      })
      var _timerC = that.data.timerC +2000;
      that.setData({
        timerC: _timerC
      })
      clearTimeout(ontimer);
      if (that.data.jishu < 30) {
        var _jishu = that.data.jishu + 1;//题目计数
        that.setData({
          jishu: _jishu
        })
        that.loaddata();
      } else {
       that.urlsales();
      }

    }

  },
  //校验用户，根据wxid
  checkuserforwxid: function(_wxid) {
    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/mgbs/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "checkuserforwxid",
        userwxid: _wxid
      },
      method: "POST",
      success: function(res) {
        console.log("获取校验wxid的zhanghao数据：" + res.data)
        if (res.data != "") {
          var _zhanghao = res.data[0].zhanghao;
          if (that.data.mode == _zhanghao) {
            console.log("该用户剩余次数：" + res.data[0].cishu)
            if (res.data[0].cishu != 0) {
              that.updateusercishu(_wxid, _zhanghao); //更新次数

            } else {
              that.messhow("您的参赛次数已用完，敬请期待排行！3秒后返回登录页面！")
              that.backlogin();
            }
          } else {
            that.messhow("请输入微信绑定的帐号！3秒后返回登录页！")
            that.backlogin();
          }

        } else {
          that.updateusername(_wxid, that.data.mode); //更新用户信息
          console.log("数据库没有此ID，新用户")
        }
      },
      fail: function(e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })

  },
  //更新用户，根据zhanghao
  updateusername: function(_wxid, _zhanghao) {

    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/mgbs/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "updateusername",
        userwxid: _wxid,
        zhanghao: _zhanghao
      },
      method: "POST",
      success: function(res) {
        console.log("获取更新用户结果数据：" + res.data)
        if (res.data == 1) {

          that.updateusercishu(_wxid, _zhanghao); //更新次数
        } else {
          that.messhow("更新用户失败，请联系管理员！3秒后返回登录页！")
          that.backlogin();
          console.log("更新新用户失败")
        }
      },
      fail: function(e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })

  },
  //更新分数，根据zhanghao
  updateusercishu: function(_wxid, _zhanghao) {

    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/mgbs/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "updateusercishu",
        userwxid: _wxid,
        zhanghao: _zhanghao
      },
      method: "POST",
      success: function(res) {
        console.log("获取更新次数结果数据：" + res.data)
        if (res.data == 1) {

          that.loaddata(); //加载题目

        } else {

          console.log("更新次数失败")
        }
      },
      fail: function(e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })

  },
  //加载题目
  loaddata: function() {

    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/mgbs/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "gettimu",
      },
      method: "POST",
      success: function(res) {
        console.log("获取题目数据：" + res.data.length)
        if (res.data != "") {
          console.log("上一题题目编号:" + that.data.num + "本题题目编号:" + res.data[0].num)
          if (that.data.num == res.data[0].num) {
            console.log("重新获取题目。。。。。。")
                  that.loaddata();
          } else {
            that.setData({
              num: res.data[0].num,
              timu: res.data[0].timu,
              ansa: res.data[0].ansa,
              ansb: res.data[0].ansb,
              ansc: res.data[0].ansc,
              ansd: res.data[0].ansd,
              anstrue: res.data[0].anstrue,
              datifun: "btanda",
            })
            console.log("加载题目成功：" + that.data.datifun)
            that.setData({
              timer: 2000,
              flag:false
            })
            that.countdown();
          }

        } else {
          that.messhow("加载题目失败。。。联系管理员");
          console.log("加载题目失败")
        }
      },
      fail: function(e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })

  },
  //答题点击
  btanda: function(event) {

    var that = this;
   if(that.data.flag==false){ //判断是否已答题完
     that.setData({
       flag:true
     })
    if (that.data.anstrue != "") {
    
  
        var daan = event.currentTarget.dataset['daan'];//获取点击按钮的选项
        if (daan == that.data.anstrue) { //匹配选项和正确答案
          var _fsJiShu = that.data.fsJiShu + 1;  //分数变量加1
          that.setData({
            fsJiShu: _fsJiShu
          })
          //答对加分
          clearTimeout(ontimer);  //停止计时器
          var shijian = 2000 - that.data.timer;  //计算所用时间
          var _timerC = that.data.timerC + shijian;//计算累计时间
          that.setData({
            timerC: _timerC
          })
          console.log("当前消耗时间：" + shijian)
          that.setData({
            tishi: "您答对了，答案:" + that.data.anstrue + " 3秒后进入下一题！"
          })
          setTimeout(function () {  
            //要延时执行的代码
            console.log("延迟执行代码")
      
          if (that.data.modeC == false) {
            //that.setfenshuforwxidzhanghao(that.data.userwxid, that.data.mode, "1", shijian); //插入分数
            that.updateuserfenshu(that.data.userwxid, that.data.mode, "1", shijian); //更新分数
         
          } else {
            if (that.data.jishu<30){
              var _jishu = that.data.jishu + 1;//题目计数
              that.setData({
                jishu: _jishu
              })
              that.loaddata();
            }else{
             that.urlsales();
            }
          

          }
          }, 4000) 

        } else {
          that.setData({
            tishi: "正确答案:" + that.data.anstrue+" 3秒后进入下一题！"
          })
          clearTimeout(ontimer);
          setTimeout(function () {
            //要延时执行的代码
            console.log("延迟执行代码")
            if (that.data.jishu < 30) {
              var _jishu = that.data.jishu + 1;//题目计数
              that.setData({
                jishu: _jishu
              })
              that.loaddata();
            } else {
             that.urlsales();
            }
          }, 4000) 

          console.log("答错进入下一题")
          //答错直接进入下一题
        }
      
    } else {
      that.messhow("题目获取错误，请联系管理员");
      that.backlogin();
    }
   }
  },
  
  //更新分数
  updateuserfenshu: function (_wxid, _zhanghao, _fenshu, _shijian) {

    var that = this
    wx.request({
      url: 'https://www.citytk.com/citytk/mgbs/modeht/mode.php',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        mode: "updateuserfenshu",
        userwxid: _wxid,
        zhanghao: _zhanghao,
        fenshu: _fenshu,
        shijian: _shijian
      },
      method: "POST",
      success: function (res) {
        console.log("插入分数和时间结果：" + res.data)
        if (res.data != "") {
          clearTimeout(ontimer);
          if (that.data.jishu < 30) {
            var _jishu = that.data.jishu + 1;//题目计数
            that.setData({
              jishu: _jishu
            })
            that.loaddata();
          } else {
            that.urlsales();
          }
        } else {
          that.messhow("更新分数失败。。。联系管理员");
          that.backlogin();
        }
      },
      fail: function (e) {
        that.messhow("数据连接失败。。。联系管理员");
        console.log("数据联系失败e:" + e.errMsg)
      }
    })

  },
  
  //返回首页
  backlogin: function() {
    var that = this;

    ontimer = setTimeout(function() {
      wx.reLaunch({
        url: '../city/city',
      })
    }, 3000);

    console.log("返回首页。。")
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearTimeout(ontimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearTimeout(ontimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */

  bindFormSubmit: function(e) {
    wx.redirectTo({
      url: '../city/city'
    })
    console.log("F:商家首页")
  }
})